package edu.szcu.vitaguard.dao;

import edu.szcu.vitaguard.entity.Doctor;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 医生DAO接口
 */
@Mapper
public interface DoctorDao {

    /**
     * 插入医生
     * @param doctor 医生对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO doctor(doctor_name, title, dept_id, introduction, status, create_time, update_time) " +
            "VALUES(#{doctorName}, #{title}, #{deptId}, #{introduction}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "doctorId")
    int insert(Doctor doctor);

    /**
     * 根据ID查询医生
     * @param doctorId 医生ID
     * @return 医生对象
     */
    @Select("SELECT d.*, dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM doctor d " +
            "LEFT JOIN department dept ON d.dept_id = dept.dept_id " +
            "WHERE d.doctor_id = #{doctorId}")
    @Results({
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "doctorName", column = "doctor_name"),
        @Result(property = "title", column = "title"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "introduction", column = "introduction"),
        @Result(property = "status", column = "status"),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    Doctor selectById(Integer doctorId);

    /**
     * 查询所有医生
     * @return 医生列表
     */
    @Select("SELECT d.*, dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM doctor d " +
            "LEFT JOIN department dept ON d.dept_id = dept.dept_id")
    @Results({
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "doctorName", column = "doctor_name"),
        @Result(property = "title", column = "title"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "introduction", column = "introduction"),
        @Result(property = "status", column = "status"),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Doctor> selectAll();

    /**
     * 根据科室ID查询医生
     * @param deptId 科室ID
     * @return 医生列表
     */
    @Select("SELECT d.*, dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM doctor d " +
            "LEFT JOIN department dept ON d.dept_id = dept.dept_id " +
            "WHERE d.dept_id = #{deptId}")
    @Results({
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "doctorName", column = "doctor_name"),
        @Result(property = "title", column = "title"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "introduction", column = "introduction"),
        @Result(property = "status", column = "status"),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Doctor> selectByDeptId(Integer deptId);

    /**
     * 根据医生姓名查询医生
     * @param doctorName 医生姓名
     * @return 医生列表
     */
    @Select("SELECT d.*, dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM doctor d " +
            "LEFT JOIN department dept ON d.dept_id = dept.dept_id " +
            "WHERE d.doctor_name LIKE CONCAT('%', #{doctorName}, '%')")
    @Results({
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "doctorName", column = "doctor_name"),
        @Result(property = "title", column = "title"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "introduction", column = "introduction"),
        @Result(property = "status", column = "status"),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Doctor> selectByName(String doctorName);

    /**
     * 根据状态查询医生
     * @param status 状态
     * @return 医生列表
     */
    @Select("SELECT d.*, dept.dept_name, dept.description as dept_description, dept.status as dept_status " +
            "FROM doctor d " +
            "LEFT JOIN department dept ON d.dept_id = dept.dept_id " +
            "WHERE d.status = #{status}")
    @Results({
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "doctorName", column = "doctor_name"),
        @Result(property = "title", column = "title"),
        @Result(property = "deptId", column = "dept_id"),
        @Result(property = "introduction", column = "introduction"),
        @Result(property = "status", column = "status"),
        @Result(property = "department", column = "dept_id", javaType = edu.szcu.vitaguard.entity.Department.class,
                one = @One(select = "edu.szcu.vitaguard.dao.DepartmentDao.selectById"))
    })
    List<Doctor> selectByStatus(String status);

    /**
     * 更新医生
     * @param doctor 医生对象
     * @return 影响的行数
     */
    @Update("UPDATE doctor SET doctor_name = #{doctorName}, title = #{title}, " +
            "dept_id = #{deptId}, introduction = #{introduction}, status = #{status}, " +
            "update_time = #{updateTime} WHERE doctor_id = #{doctorId}")
    int update(Doctor doctor);

    /**
     * 删除医生
     * @param doctorId 医生ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM doctor WHERE doctor_id = #{doctorId}")
    int deleteById(Integer doctorId);
}
